import { ref } from "vue"
export const useSearchEvent = (map, docName) => {
  // 定义服务
  let service = {
    name: docName,
    layerId: 2,
  }
  /* 创建一个空的高亮图层 */
  var source = new ol.source.Vector({
    wrapX: false,
  })
  let layer = new ol.layer.Vector({
    source,
    style: new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        /* 填充色 */
        fill: new ol.style.Fill({
          color: "rgba(255, 0, 0,.9)",
        }),
        /* 描边 */
        stroke: new ol.style.Stroke({
          color: "skyblue",
          width: 2,
        }),
      }),
    }),
  })
  // 在map中添加layer
  setTimeout(() => {
    map.value.addLayer(layer)
  }, 200)
  //定义查询的列表
  const searchList = ref([])
  //定义显示对话框的变量
  const dialogFlag = ref(false)
  const searchEvent = (searchContent) => {
    dialogFlag.value = true
    let where = `事件类型 like '%${searchContent.value}%' or 发生时间 like '%${searchContent.value}%' or 发生地点 like '%${searchContent.value}%' or 车牌号 like '%${searchContent.value}%' or 驾驶员 like '%${searchContent.value}%'`
    Query.queryByAttr({
      where,
      service,
      callback: (res) => {
        try {
          // 将其转为ol要素
          var format = new Zondy.Format.PolygonJSON()
          //将MapGIS要素JSON反序列化为ol.Feature类型数组
          var features = format.read(res)
          // 将查询到的要素添加到高亮图层中
          source.addFeatures(features)
          if (searchList.value) {
            searchList.value = []
          }
          res.SFEleArray.forEach((item) => {
            if (item.AttValue[7] == 0) {
              item.AttValue[7] = "未处理"
            } else if (item.AttValue[7] == 1) {
              item.AttValue[7] = "已忽略"
            } else {
              item.AttValue[7] = "已通过"
            }
            searchList.value.push({
              事件编号: item.AttValue[0],
              事件类型: item.AttValue[1],
              事件等级: item.AttValue[2],
              发生时间: item.AttValue[3],
              发生地点: item.AttValue[4],
              车牌号: item.AttValue[5],
              驾驶员: item.AttValue[6],
              处理状态: item.AttValue[7],
            })
          })
        } catch (error) {
          searchList.value = []
        }
      },
    })
  }
  const closeDialog = () => {
    dialogFlag.value = false
    source.clear()
  }
  return { searchEvent, searchList, dialogFlag, closeDialog }
}
